"""
文档分享模型
"""
from sqlalchemy import Column, String, DateTime, ForeignKey, Enum as SQLEnum
from sqlalchemy.orm import relationship
from datetime import datetime
import enum
import uuid

from app.models.database import Base


class SharePermission(str, enum.Enum):
    """分享权限枚举"""
    VIEW = "view"  # 仅查看
    DOWNLOAD = "download"  # 查看 + 下载
    COMMENT = "comment"  # 查看 + 评论


class DocumentShare(Base):
    """文档分享表"""
    __tablename__ = "document_shares"

    id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
    document_id = Column(String, ForeignKey("documents.id", ondelete="CASCADE"), nullable=False, index=True)
    shared_by_id = Column(String, ForeignKey("users.id"), nullable=False)
    shared_to_id = Column(String, ForeignKey("users.id"), nullable=False, index=True)
    permission = Column(SQLEnum(SharePermission), default=SharePermission.VIEW, nullable=False)
    expires_at = Column(DateTime, nullable=True)  # 过期时间，null表示永不过期
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)

    # 关系
    document = relationship("Document", back_populates="shares")
    shared_by = relationship("User", foreign_keys=[shared_by_id])
    shared_to = relationship("User", foreign_keys=[shared_to_id])

